<?php
/**
 * 体检项目分类
 *
 * User: kaikaic
 * Date: 2018/12/11
 * Time: 20:45
 */

namespace app\exam\controller;

use app\exam\model\ItemClassModel;
use app\exam\service\MecService;
use app\exam\service\MecItemService;
use app\exam\service\MecSetService;
use controller\BasicAdmin;
use service\ConfigService;
use think\Db;

class ItemSet extends BasicAdmin {

    protected $table = "ExamItemClass";

    /**
     * 体检项目-列表
     */
    public function index() {
        $this->title = "项目分类";

        $name = $this->request->get("name");
        $mecID = intval($this->request->param("mec_id"));

        $query = array();
        if ($mecID) {
            $query['mec_id'] = $mecID;
        }
        if ($name) {
            $query['name'] = array('like', "%{$name}%");
        }

        parent::listRecordByPage($query, "", "id desc");
    }
    public function import() {
        if ($this->request->isGet()) {
            return view('import', [
                'title'=>'导入医院体检项目'
            ]);
        }

        if ($this->request->isPost()) {
            if ($this->doImportAction()) {
                $this->success('导入完成！', "");
            }else {
                $this->error('导入失败，请稍候再试！');
            }
        }
    }
    private function doImportAction() {
        $data = $this->request->post();
        $excelFile = $data['item_class_excel'];
        if (!$excelFile) {
            return false;
        }

        $ItemModel = new ItemClassModel();
        $ItemModel->readExcelFile($excelFile);

        return true;
    }
    /**
     * 体检项目分类-添加
     */
    public function add() {
        if ($this->request->isGet()) {
            $vo = array();
            return view('form', [
                'title'=>'新增体检项目分类',
                'vo'=>$vo,
                'mec_list'=>MecService::getInstance()->listMec()
            ]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->doInsertCateAction($data)) {
                $this->success('新增成功！', '');
            }else {
                $this->error('新增失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检项目分类-编辑
     */
    public function editCate() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $itemClass = MecItemService::getInstance()->find($id);
            if (empty($itemClass)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', [
                'title'=>'编辑体检项目分类',
                'vo'=>$itemClass,
                'mec_list'=>MecService::getInstance()->listMec()
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertCateAction($data)) {
                $this->showSuccess('编辑成功！', '/exam/item/cate');
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检项目分类-删除
     */
    public function removeCate() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $itemClass = MecItemService::getInstance()->find($id);
        if (empty($itemClass)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = MecItemService::getInstance()->remove(array('id'=>$id));
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }


    /**
     * 数据写入
     */
    protected function doInsertCateAction($data, $ids = []) {
        if (empty($data['id'])) {
            $data['sort'] = MecItemService::getInstance()->getMaxSort() + 1;
            $data['status'] = 1;

            $result = MecItemService::getInstance()->insert($data);
        } else {
            $id = intval($data['id']);
            $result = MecItemService::getInstance()->update($data, $id);
        }

        return $result;
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);

        for ($i=0; $i<$size; $i++) {
            $mecID = $data[$i]['mec_id'];

            $mec = MecService::getInstance()->find($mecID);
            if (!$mec) {
                continue;
            }
            $data[$i]['mec'] = $mec['name'];

            $data[$i]['item_num'] = MecItemService::getInstance()->count(array('item_set_id'=>$data[$i]['id']), 1);
        }

        $mecList = MecService::getInstance()->listMec();
        $this->assign("mec_list", $mecList);
    }
}

